package verins.DAO;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.swing.JOptionPane;
import verins.beans.Permiso;
import verins.beans.Rol;

public class PermisoDAO {
    public List findAll(){
        List<Permiso> lista=null;
        try{

            EntityManagerFactory emf = Persistence.createEntityManagerFactory("verinsPU");
            EntityManager em = emf.createEntityManager();
            Query query=em.createNamedQuery("Permiso.findAll");

            lista=query.getResultList();

            if (em.getTransaction().isActive())
                em.getTransaction().commit();
            em.close();
            emf.close();

        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
        return lista;
    }

    public List findByRol(Rol rol){
        List<Permiso> lista=null;
        try{
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("verinsPU");
            EntityManager em = emf.createEntityManager();
            String strSelect=
                    "SELECT p FROM PermisoXRol p";
            Query query= em.createQuery(strSelect);
            

            /*Query query = em.createNativeQuery("SELECT p "
                + "FROM permiso p INNER JOIN permisoxrol pxr ON p.idrol = pxr.idrol "
                + "WHERE idrol= "+ rol.getIdrol(), Permiso.class);
*/
            lista=query.getResultList();

            if (em.getTransaction().isActive())
                em.getTransaction().commit();
            em.close();
            emf.close();

        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
        return lista;
    }
}
